home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / misc / wolf3d.lha / WOLF3D / functions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  8.2 KB  |  339 lines

  1. #include "game.h"
  2. #include "joystick.h"
  3.  
  4. #define FFPBitSize 14
  5. #define SINE_TABLE_SIZE 900
  6.  
  7. long TBlock[ NbrBlocks ][2];
  8. word Twalls[8][65][2];
  9. byte walls[8][65][2];
  10.  
  11. extern struct BLOCKTYPE Level_One[ NbrBlocks ];
  12. extern struct xBUFFTYPE xBUFFER[ViewRight+1];
  13.  
  14. extern word zBUFFER[NbrBlocks + NbrObjects][2];
  15. extern word *COSINE, SINE[SINE_TABLE_SIZE+1];
  16. extern word INPUTMETHOD;
  17. extern word FIN;
  18.  
  19. void Rotate(long *, long *, word );
  20. void UpdatePlayerPosition( struct PLAYER * );
  21. void TransformBlocks( struct PLAYER * );
  22. word CreateZbuffer( void );
  23. void CreateXbuffer( word );
  24. void FetchEvaluateInput( struct PLAYER * );
  25. word GetIDCMP( void );
  26.  
  27. /* -------------------------------------------------------------- */
  28.  
  29. void CreateXbuffer( word zCount)
  30. {
  31.     extern struct xBUFFTYPE xBUFFER[ViewRight+1];
  32.     register long index, Tindex, z, x, y;
  33.     word WALLDOOR, wall, face;
  34.     word xAccess = 0;
  35.     word aX,aZ,cX,cZ;
  36.     word done, loop;
  37.     word column, lastBMP;
  38.     
  39.     long lastY,lastX;
  40.     
  41.     for(index = ViewLeft; index <= ViewRight; index++) xBUFFER[index].height = 0;
  42.  
  43.     for(index = 0; index < zCount; index++)
  44.     {
  45.     Tindex = zBUFFER[index][0];
  46.  
  47.     if( Level_One[Tindex].flags & (1 << (W+1)) ) WALLDOOR = 4;
  48.     else WALLDOOR = 0;
  49.     
  50.     for(wall = N; wall <= W; wall++)
  51.     {
  52.         if( Level_One[Tindex].flags & (1 << wall) )
  53.         {
  54.         face = wall + WALLDOOR;
  55.  
  56.         cX = -(Twalls[face][0][0] + TBlock[Tindex][0]);
  57.         cZ =  Twalls[face][0][1]  + TBlock[Tindex][1];
  58.             
  59.         aX =  Twalls[face][63][0] + TBlock[Tindex][0];
  60.         aZ =  Twalls[face][63][1] + TBlock[Tindex][1];
  61.  
  62.         if( ((cX*aZ)+(cZ*aX)) > 0)
  63.         {
  64.             column = 1;
  65.             done = 0;
  66.  
  67.             x = Twalls[face][0][0] + TBlock[Tindex][0];
  68.             z = Twalls[face][0][1] + TBlock[Tindex][1];
  69.  
  70.             if( (wall == 0) || (wall == 2) ) lastBMP = (long)Level_One[Tindex].bmp1;
  71.             else lastBMP = (long)Level_One[Tindex].bmp2;
  72.  
  73.             if(z > 0)
  74.             {
  75.             x = ( x<< 9)/(z+z+z) + ViewCenterX;
  76.             lastY = (40<<14)/(z+z+z);
  77.             }
  78.             else
  79.             {
  80.             x = x + ViewCenterX;
  81.             lastY = ViewBottom-ViewCenterY;
  82.             };
  83.  
  84.             lastX = x;
  85.  
  86.             if(x < ViewLeft)
  87.             {
  88.             lastX = ViewLeft;
  89.             }
  90.             else if(x > ViewRight)
  91.             {
  92.             done = TRUE;
  93.             };
  94.  
  95.             while( ! done)
  96.             {
  97.             x = Twalls[face][column][0] + TBlock[Tindex][0];
  98.             z = Twalls[face][column][1] + TBlock[Tindex][1];
  99.     
  100.             if(z > 0)
  101.             {
  102.                 x = ( x<< 9)/(z+z+z) + ViewCenterX;
  103.                 y = (40<<14)/(z+z+z);
  104.             }
  105.             else
  106.             {
  107.                 x = x + ViewCenterX;
  108.                 y = ViewBottom-ViewCenterY;
  109.             };
  110.     
  111.             if(  x < ViewLeft  )
  112.             {
  113.                 x = ViewLeft-1;
  114.                 lastX = x;
  115.                 lastY = y;
  116.                 if( (wall == 0) || (wall == 2) ) lastBMP = (long)Level_One[Tindex].bmp1;
  117.                 else lastBMP = (long)Level_One[Tindex].bmp2;
  118.             }
  119.             else if( x > ViewRight)
  120.             {
  121.                 x = ViewRight + 1-1;
  122.                 z = 1;
  123.                 done = TRUE;
  124.             };
  125.     
  126.             if(z > 0)
  127.             {
  128.                 for(loop = lastX; loop <= x; loop++)
  129.                 {
  130.                 if(xBUFFER[loop].height < lastY)
  131.                 {
  132.                     if(xBUFFER[loop].height == 0) xAccess++;
  133.                     xBUFFER[loop].height = lastY;
  134.                     xBUFFER[loop].column = column-1;
  135.                     xBUFFER[loop].bitmap = (long *)Wall[lastBMP];
  136.                 };
  137.                 };
  138.             };
  139.                 
  140.             lastX = x;
  141.             lastY = y;
  142.  
  143.             if( (wall == 0) || (wall == 2) ) lastBMP = (long)Level_One[Tindex].bmp1;
  144.             else lastBMP = (long)Level_One[Tindex].bmp2;
  145.             
  146.             column++;
  147.             if(column > 64) done = TRUE;
  148.  
  149.             };// while not done
  150.  
  151.         };// if dot product
  152.  
  153.         };// if wall exists
  154.  
  155.     };// for each wall
  156.  
  157.     if(xAccess == (ViewRight - ViewLeft + 1)) index = zCount;
  158.  
  159.     };// for each block not behind the viewer
  160. }
  161.  
  162. /* -------------------------------------------------------------------------- */
  163.  
  164. word CreateZbuffer( void )
  165. {
  166.     register word zCount, index;
  167.     
  168.     /* ---------------------------------------------------------------- */
  169.     /* ADD PARTIALLY VISIBLE BLOCKS TO THE zBUFFER            */
  170.     /* ---------------------------------------------------------------- */
  171.  
  172.     zCount = 0;        
  173.     for(index = 0; index < NbrBlocks; index++)
  174.     {
  175.     if( TBlock[index][1] > (-32 << 5))
  176.     {
  177.         zBUFFER[zCount][0] = index;           /* Add block if within */
  178.         zBUFFER[zCount][1] = TBlock[index][1]; /* player's 90 degree  */
  179.         zCount++;                   /* peripheral vision.  */
  180.         
  181.     };
  182.     };
  183.  
  184. /*
  185.     /* ---------------------------------------------------------------- */
  186.     /* ADD PARTIALLY VISIBLE OBJECTS TO zBUFFER                */
  187.     /* ---------------------------------------------------------------- */
  188.  
  189.         for(index = 0; index < NbrObjects; index++)
  190.         {
  191.             Object[index].draw = FALSE;
  192.             
  193.             Object[index].transX = Object[index].center[0] - playerX;
  194.             Object[index].transZ = Object[index].center[1] - playerZ;
  195.  
  196.             Rotate(&Object[index].transZ, &Object[index].transX, player[playerDir]);
  197.  
  198.             if( Object[index].transZ > 32)
  199.             {               
  200.                 zBUFFER[zCount][0] = 10000+index; // object key.
  201.                 zBUFFER[zCount][1] = Object[index].transZ;
  202.                 zCount++;
  203.             };
  204.         };
  205. */
  206.  
  207.     return ((word)zCount);
  208.  
  209. }
  210.  
  211. /* -------------------------------------------------------------------------- */
  212.  
  213. void TransformBlocks( struct PLAYER *player )
  214. {
  215.     register long index, wall, tpt1, tpt2, Degree;
  216.     
  217.     Degree = player->attitude;
  218.     
  219.     for(index = 0; index < NbrBlocks; index++)
  220.     {
  221.     tpt1 = ((long) Level_One[index].z) - player->Zcopy;
  222.     tpt2 = ((long) Level_One[index].x) - player->Xcopy;
  223.  
  224.     TBlock[index][1] = ((COSINE[Degree] * tpt1) - (SINE[Degree] * tpt2)) >> 9;
  225.     TBlock[index][0] = ((COSINE[Degree] * tpt2) + (SINE[Degree] * tpt1)) >> 9;
  226.     };
  227.  
  228.     for(wall = N; wall <= (W+4); wall++)
  229.     {
  230.     for(index = 0; index <= 64; index++)
  231.     {
  232.         tpt2 = walls[wall][index][0];
  233.         tpt1 = walls[wall][index][1];
  234.  
  235.         Twalls[wall][index][0] = (word) (((COSINE[Degree] * tpt2) + (SINE[Degree] * tpt1)) >> 9);
  236.         Twalls[wall][index][1] = (word) (((COSINE[Degree] * tpt1) - (SINE[Degree] * tpt2)) >> 9);
  237.     };
  238.     };
  239. }
  240.  
  241. /* -------------------------------------------------------------------------- */
  242.  
  243. void UpdatePlayerPosition( struct PLAYER *player )
  244. {
  245.     register word Degree;
  246.     
  247.     if(player->attitude > 720) player->attitude -= 720;
  248.     else if(player->attitude < 0) player->attitude += 720;
  249.  
  250.     Degree = player->attitude;
  251.  
  252.     player->X = (player->X - (player->speed *   SINE[Degree]));
  253.     player->Z = (player->Z + (player->speed * COSINE[Degree]));
  254.     player->speed = 0;
  255.         
  256.     player->Xcopy = (word)(player->X >> FFPBitSize);
  257.     player->Zcopy = (word)(player->Z >> FFPBitSize);
  258. }
  259.  
  260. /* -------------------------------------------------------------------------- */
  261.  
  262. void Rotate(long *pt1, long *pt2, short Degree)
  263. {
  264.     register long tpt1, tpt2, tpt3;
  265.  
  266.     tpt1 = *pt1;
  267.     tpt2 = *pt2;
  268.  
  269.     tpt3 = ((COSINE[Degree] * tpt1) - (SINE[Degree] * tpt2)) >> 9;//FFPBitSize;
  270.     *pt1 = tpt3;
  271.     
  272.     tpt3 = ((COSINE[Degree] * tpt2) + (SINE[Degree] * tpt1)) >> 9;//FFPBitSize;
  273.     *pt2 = tpt3;
  274. }
  275.  
  276. /* -------------------------------------------------------------------------- */
  277.  
  278. void FetchEvaluateInput( struct PLAYER *player )
  279. {
  280.     register word code;
  281.     
  282.     code = GetIDCMP();
  283.  
  284.     if(code == ESC) FIN = ESC;
  285.     else if(INPUTMETHOD == KEYBOARD)
  286.     {
  287.     }
  288.     else // INPUTMETHOD == JOYSTICK
  289.     {
  290.     code = ReadJoystick();
  291.  
  292.         if(code & LBUTTON)  // FireButtonStatus
  293.         {
  294.         FIN = TRUE;
  295.         };
  296.         
  297.         if(code & YJOYUP)
  298.         {
  299.             player->speed = 8; // move forwards
  300.         }
  301.         else if(code & YJOYDWN)
  302.         {
  303.             player->speed = -8;// move back
  304.         };
  305.         
  306.         if(code & XJOYLFT)
  307.         {
  308.             player->attitude += (5*2); // 2 = 1 degree rotate left
  309.         }
  310.         else if(code & XJOYRGT)
  311.         {
  312.             player->attitude -= (5*2); // 2 = 1 degree rotate right
  313.         };
  314.     };
  315. }
  316.  
  317. /* -------------------------------------------------------------------------- */
  318.  
  319. struct IntuiMessage *MyMSG;
  320.  
  321. word GetIDCMP( void )
  322. {
  323.     word code = 0;
  324.  
  325.     while(MyMSG = (struct IntuiMessage *) GetMsg(MyWIN->UserPort))
  326.     {
  327.         if((MyMSG->Class == IDCMP_VANILLAKEY) || (MyMSG->Class == IDCMP_RAWKEY))
  328.         {
  329.             code = MyMSG->Code;
  330.         };
  331.     
  332.         ReplyMsg((struct Message *)MyMSG);
  333.     };
  334.  
  335.     return((word) code);
  336. }
  337.  
  338. /* -------------------------------------------------------------------------- */
  339.